package com.qingxin.park.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Service;

import com.qingxin.park.entity.FarmPark;
import com.qingxin.park.entity.Material;
import com.qingxin.park.entity.MaterialType;

import com.qingxin.sys.dao.Querier;
import com.qingxin.sys.dao.Updater;
import com.qingxin.sys.entity.LoginUser;
import com.qingxin.sys.mvc.PageParam;
import com.qingxin.sys.mvc.Pager;
import com.qingxin.sys.mvc.SysError;
import com.qingxin.sys.service.BaseService;
import com.qingxin.sys.support.UploadFile;
import com.qingxin.sys.utils.BeanUtil;
import com.qingxin.sys.utils.StringUtils;
import com.qingxin.ui.bean.TreeNode;

/**
 * 物资
 * @author Administrator
 *
 */
@Service
public class MaterialService extends BaseService<Material, String> {

	/**
	 * 获取物资类别数据
	 * @param parkid
	 * @param typeid
	 * @param isParent
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<TreeNode> getUITreeData(String parkid, String typeid, boolean isParent){
		List<TreeNode> list = new ArrayList<TreeNode>();
		
		StringBuilder hql = new StringBuilder();
		hql.append("select new com.qingxin.park.entity.MaterialType(c.materialid, c.materialName)");
		hql.append(" from MaterialType c where c.materialType.typeid=?");
		hql.append(" order by c.materialName asc");
		List<MaterialType> material = listByHql(hql.toString(), typeid);
		if(material != null){
			for(MaterialType c : material){
				list.add(new TreeNode(c.getTypeid(), c.getTypeName(), "MATERIALTYPE", null, isParent));
			}
		}
		return list;
	}
	
	
	@SuppressWarnings("unchecked")
	public List<Material> listByType(String parkid, String typeid){
		StringBuilder hql = new StringBuilder();
		hql.append("from Material m where m.farmPark.parkid=? and m.materialType.typeid=?");
		hql.append(" order by m.materialName asc");
		List<Material> materialList = listByHql(hql.toString(), parkid, typeid);
		return materialList;
	}
	
	
	/**
	 * 获取物资全名：分类名 > 物资名
	 * @param materialid
	 * @return
	 */
	public String getMaterialFullname(String materialid){
		Material material = get(materialid);
		if(material != null){
			return material.getMaterialType().getTypeName() +" > "+ material.getMaterialCode();
		}
		return null;
	}
	
	
	/**
	 * 列表数据
	 * @param materialid
	 * @param typeid
	 * @param materialName
	 * @param pageParam
	 * @param loginUser
	 * @return
	 */
	
	public Pager list(String materialid, String typeid, String materialName, PageParam pageParam, LoginUser loginUser){
		Querier querier = Querier.create("select new com.qingxin.park.entity.Material(" +
				"c.materialid, c.materialCode, c.materialName, c.pbn, c.units, c.factory, c.createTime, t.typeName,p.parkName)" +
				" from Material c, MaterialType t, FarmPark p where c.materialType.typeid=t.typeid and c.farmPark.parkid=p.parkid");
		
		if("2".equals(loginUser.getUser_type())){
			querier.append(" and c.farmPark.parkid in (select distinct v.farmPark.parkid from SysUserParkRel v where v.sysUser.userid='"+loginUser.getUserid()+"')");
		}
		
		if(StringUtils.isNotEmpty(typeid)){
			querier.append(" and c.materialType.typeid=:typeid");
			querier.setParam("typeid", typeid);
		}
		if(StringUtils.isNotEmpty(materialName)){
			materialName = materialName.replace("'", "");
			querier.append(" and (c.materialName like :materialName or c.materialCode like :materialCode) ");
			querier.setParam("materialName", "%"+materialName+"%");
			querier.setParam("materialCode", "%"+materialName+"%");
		}
		querier.append(pageParam.getOrderby(" order by c.materialName asc", "c"));
		Pager pager = pageByQuerier(querier, pageParam);
		return pager;
	}
	
	
	/**
	 * 添加物资信息
	 * @param material
	 * @param typeid
	 * @param loginUser
	 * @param request
	 * @return
	 */
	public SysError add(Material material, String typeid, String parkid, LoginUser loginUser, HttpServletRequest request){
		material.setMaterialType(new MaterialType(typeid));
		material.setFarmPark(new FarmPark(parkid));
		Date now = new Date();
		material.setCreateTime(now);
		material.setUpdateTime(now);
		material.setCreateBy(loginUser.getLogin_name());
		material.setUpdateBy(loginUser.getLogin_name());
		
		if(StringUtils.isNotEmpty(material.getMaterialImg())){
			UploadFile.moveTmpTo(material.getMaterialImg(), UploadFile.DIR_MATERIAL_IMG);
		}
		
		this.save(material);
		return null;
	}
	
	
	/**
	 * 修改物资信息
	 * @param material
	 * @param loginUser
	 * @param request
	 * @return
	 */
	public SysError update(Material material, LoginUser loginUser, HttpServletRequest request){
		Material old = getSafeEntity(material.getMaterialid());
		
		if(StringUtils.isNotEmpty(material.getMaterialImg()) && !material.getMaterialImg().equals(old.getMaterialImg())){
			UploadFile.moveTmpTo(material.getMaterialImg(), UploadFile.DIR_MATERIAL_IMG);
		}
		
		String fields = "materialCode,materialName,pbn,units,factory,remark,materialImg";
		BeanUtil.setTargetFields(old, material, fields, true);
		
		old.setUpdateBy(loginUser.getLogin_name());
		old.setUpdateTime(new Date());
		Updater<Material> updater = new Updater<>(old, fields+",updateBy,updateTime");
		this.update(updater);
		return null;
		
	}
	
	
	/**
	 * 删除农作物信息
	 * @param materialids
	 * @param loginUser
	 * @param request
	 * @return
	 */
	public Object delete(String[] materialids, LoginUser loginUser, HttpServletRequest request){
		for(String materialid : materialids){
			Material material = get(materialid);
			if(material==null){
				continue;
			}
			this.delete(material);
		}
		
		return null;
	}
	
	/**
	 * 判断指定名称是否被使用
	 * @param materialName
	 * @param materialid
	* @param typeid
	 * @return
	 */
	public boolean isNameDuplicate(String materialName, String materialid, String typeid){
		if(StringUtils.isEmpty(materialid)){
			Object obj = getByHql("select materialid from Material where materialName=? and materialType.typeid=?", materialName,typeid);
			return obj!=null;
		}else {
			Object obj = getByHql("select materialid from Material where materialid<>? and materialName=? and materialType.typeid=?", materialid, materialName, typeid);
			return obj!=null;
		}
	}
	
	/**
	 * 判断指定CODE是否被使用
	 * @param materialCode
	 * @param materialid
	 * @param typeid
	 * @return
	 */
	public boolean isCodeDuplicate(String materialCode, String materialid, String typeid){
		if(StringUtils.isEmpty(materialid)){
			Object obj = getByHql("select materialid from Material where materialCode=? and materialType.typeid=?", materialCode, typeid);
			return obj!=null;
		}else {
			Object obj = getByHql("select materialid from Material where materialid<>? and materialCode=? and materialType.typeid=?", materialid, materialCode, typeid);
			return obj!=null;
		}
	}
}
